package module;

import Client.ClientInitClose;
import Client.ClientScanner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 长风无言
 */
public class ExamManage {

    private static ClientInitClose client;
    private static List<ExamQuestions> examQuestions = new ArrayList<>();

    public void addQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入题目");
        String title = ClientScanner.getScanner().nextLine();
        System.out.println("请输入选项");
        String choose = ClientScanner.getScanner().nextLine();
        System.out.println("请输入答案");
        String answer = ClientScanner.getScanner().nextLine();
        client.getOut().writeObject("add"+";"+title+";"+choose+";"+answer);
        if("success".equals(client.getIn().readObject())){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
    }

    public void deleteQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入关键字");
        String title = ClientScanner.getScanner().nextLine();
        client.getOut().writeObject("delete"+";"+title);
        if("success".equals(client.getIn().readObject())){
            System.out.println("删除成功！");
        }else{
            System.out.println("删除失败，查无此人");
        }
    }

    public void modifyQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入关键字");
        String oldtitle = ClientScanner.getScanner().nextLine();
        System.out.println("请输入新题目");
        String title = ClientScanner.getScanner().nextLine();
        System.out.println("请输入选项");
        String choose = ClientScanner.getScanner().nextLine();
        System.out.println("请输入答案");
        String answer = ClientScanner.getScanner().nextLine();
        client.getOut().writeObject("modify"+";"+oldtitle+";"+title+";"+choose+";"+answer);
        if("success".equals(client.getIn().readObject())){
            System.out.println("修改成功！");
        }else{
            System.out.println("修改失败，查无此人");
        }
    }

    public void findQuestion() throws IOException, ClassNotFoundException {
        System.out.println("请输入关键字");
        String title = ClientScanner.getScanner().nextLine();
        client.getOut().writeObject("find"+";"+title);
        while (true) {
            String str = (String) client.getIn().readObject();
            if ("success".equals(str)){
                ExamQuestions examQuestions = (ExamQuestions) client.getIn().readObject();
                System.out.println("信息如下："+examQuestions);
            } else if ("end".equals(str)) {
                return;
            } else {
                System.out.println("删除失败，查无此题");
                return;
            }
        }
    }
    public void forQuestion() throws IOException, ClassNotFoundException {
        client.getOut().writeObject("for"+";");
        examQuestions =(List<ExamQuestions>) client.getIn().readObject();
        for(ExamQuestions examQuestions:examQuestions){
            System.out.println(examQuestions.toString());
        }
    }


    public void examManage(ClientInitClose c) {

        client = c;
        while (true) {
            System.out.println("-----考题管理-----");
            System.out.println("  1、增加考题");
            System.out.println("  2、删除考题");
            System.out.println("  3、修改考题");
            System.out.println("  4、查找考题");
            System.out.println("  5、遍历考题");
            System.out.println("  0、退出考题管理");
            int choose = ClientScanner.getScanner().nextInt();
            String str = ClientScanner.getScanner().nextLine();
            try {
                switch (choose) {
                    case 1:
                        addQuestion();
                        break;
                    case 2:
                        deleteQuestion();
                        break;
                    case 3:
                        modifyQuestion();
                        break;
                    case 4:
                        findQuestion();
                        break;
                    case 5:
                        forQuestion();
                        break;
                    case 0:
                        client.getOut().writeObject("end");
                        return;
                    default:
                        System.out.println("请重新选择0-5的功能");
                        break;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
